//
//  SLNewfFeatureViewController.swift
//  0319iOSweiboDemo
//
//  Created by Tang on 16/3/22.
//  Copyright © 2016年 大天朝. All rights reserved.
//

import UIKit
import SnapKit

private let reuseIdentifier = "Cell"

class SLNewfFeatureViewController: UICollectionViewController {

    
    //流水布局
    let layout = UICollectionViewFlowLayout()
    init() {
        super.init(collectionViewLayout: layout)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        perpareCollectionView()
    }
    //按钮点击事件
    
   
    
    //CollectionView 的位置
    private func perpareCollectionView() {
        //设置layout参数
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumInteritemSpacing = 0
        layout.minimumLineSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        
        
        //注册cell
        collectionView?.registerClass(SLNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }
  
}


//MARK: - UICollectionView 数据源和代理
extension SLNewfFeatureViewController {
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return SLNweFeatuerPageCount  //4页
    }
    
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
       let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! SLNewFeatureCell
        cell.backgroundColor = UIColor.random()
        
        //设置cell要显示的页码
        cell.index = indexPath.item
        

        
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //计算当前页
        let page = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        //判断当前是否是最后一页
        if page == SLNweFeatuerPageCount - 1 {
            //创建indexPath
            let indexPath = NSIndexPath(forItem: page, inSection: 0)
            //获取idnexPath对应的cell
           let cell = collectionView?.cellForItemAtIndexPath(indexPath) as! SLNewFeatureCell
            //让开始按钮动画
            cell.startButtonAnimation()
        }
    }
    
    
}

//自定义cell
class SLNewFeatureCell: UICollectionViewCell {
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //MARK: - 属性
    //当前显示的页数
    var index: Int = 0 {
        didSet {
            //在控制器设置页码了之后.我们根据页码显示来切换对应的图片
            //index0 ,1 ,2 ,3 ,
            bkgImageView.image = UIImage(named: "new_feature_\(index + 1)")
            //只要cell要设置背景图片就隐藏按钮
            startButton.hidden = true
        }
    }
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
    }

    //开始按钮动画
    func startButtonAnimation() {
        startButton.hidden = false
        
        //在开始动画之前, 将transform设置为0
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 0.5, initialSpringVelocity: 6, options: UIViewAnimationOptions(rawValue: 6), animations: { (_) -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            }) { (_) -> Void in
               
//                print("动画完成")
        }
    
    }
    
    private func prepareUI() {
        //添加子控件
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        //添加约束
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        bkgImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(contentView)
        }
//       contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Leading, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Leading, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Trailing, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Trailing, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Top, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: bkgImageView, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: 0))
        
//     startButton.snp_makeConstraints { (make) -> Void in
//        //equalTo参照
//        make.centerX.equalTo(contentView.snp_centerX)
//        make.bottom.equalTo(contentView.snp_bottom).offset(-160)
//        }
        startButton.snp_makeConstraints { (make) -> Void in
            //当约束的view的属性和参照的view的属性一样的时候可以省略view .snp_属性
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView.snp_bottom).offset(-160)
        }
        
//        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
//        contentView.addConstraint(NSLayoutConstraint(item: startButton, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: contentView, attribute: NSLayoutAttribute.Bottom, multiplier: 1, constant: -160))
        //在写个宽高
    }
    
    //MARK: - 懒加载
    //背景
    private lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    
    private lazy var startButton: UIButton = {
        let button = UIButton()
        button.setTitle("开始体验", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        button.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Normal)
       
        button.addTarget(self, action: "didClickStarytButton", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    func didClickStarytButton() {
        print("点击了")
        //到SLMainViewController控制器
        //       let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        //        appDelegate?.window!.rootViewController = SLMainViewController()
        AppDelegate.outSwitchRootViewController(SLMainViewController())
    }
}
